package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/11/13 10:17
 */
@Component
public class SpringRabbitListener {
    @RabbitListener(queues = "simple.queue")
    public void simpleQueueListener(String msg){
        System.out.println(msg);
    }
    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg) throws InterruptedException {
        System.out.println("工作队列消费者1:"+msg);
        TimeUnit.MILLISECONDS.sleep(10);
    }
    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg) throws InterruptedException {
        System.out.println("工作队列消费者2:"+msg);
        TimeUnit.MILLISECONDS.sleep(300); //伪代码-模拟消费者处理耗时-耗时100毫秒
    }
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutQueueListener1(String msg){
        System.out.println("广播队列消费者1:"+msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutQueueListener2(String msg){
        System.out.println("广播队列消费者2:"+msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),key = {"red","blue"}))
    public void directQueueListener1(String msg){
        System.out.println("路由队列消费者1:"+msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),key = {"red","yellow"}))
    public void directQueueListener2(String msg){
        System.out.println("路由队列消费者2:"+msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),key = "*.weather"))
    public void topicQueueListener1(String msg){
        System.out.println("主题队列消费者1:"+msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),key = "chain.#"))
    public void topicQueueListener2(String msg){
        System.out.println("主题队列消费者2:"+msg);
    }
}
